﻿using System;
using System.Drawing;
using System.Windows.Forms;

namespace Common
{
    public class DataGridCheckBoxColumn : DataGridTextBoxColumn
    {


        protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
        {
            string val = PropertyDescriptor.GetValue(source.List[rowNum]).ToString();
            bool value = !(val == String.Empty || val == "False");
            PaintText(g, bounds, value, backBrush, foreBrush, alignToRight);
        }


        protected void PaintText(Graphics g, Rectangle textBounds, bool value, Brush backBrush, Brush foreBrush, bool alignToRight)
        {
            g.FillRectangle(backBrush, textBounds);
            textBounds.X += 2;
            int size;
            int boxTop;
            size = textBounds.Size.Height < textBounds.Size.Width ? textBounds.Size.Height : textBounds.Size.Width;
            size = size > ((int)g.DpiX / 7) ? ((int)g.DpiX / 7) : size;

            boxTop = textBounds.Y + (textBounds.Height - size) / 2;

            Pen p = new Pen(Color.Black);
            g.DrawRectangle(p, textBounds.X, boxTop, size, size);
            if (value)
            {
                g.DrawLine(p, textBounds.X, boxTop, textBounds.X + size, boxTop + size);
                g.DrawLine(p, textBounds.X, boxTop + size, textBounds.X + size, boxTop);
            }
        }
    }
}
